package com.ly.mpayment.persist.socket;

import java.net.InetSocketAddress;
import java.nio.channels.NotYetConnectedException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.server.ServerEndpoint;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.xsocket.connection.INonBlockingConnection;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@ServerEndpoint("/websocket")
public class WebSocketUtil extends WebSocketServer {
	// new ConcurrentHashMap<String, INonBlockingConnection>();
	public static Map<String, List<WebSocket>> socketMap = Maps.newHashMap();// 频道-进入频道用户的连接列表

	public WebSocketUtil(InetSocketAddress address) {
		super(address);
	}

	/**
	 * 
	 * @param address
	 * @param decoders用于限制websocket版本号
	 */
	public WebSocketUtil(InetSocketAddress address, int decoders) {
		super(address, decoders);
	}

	@Override
	public void onOpen(WebSocket conn, ClientHandshake handshake) {
		System.out.println("onOpen");
		String desc = handshake.getResourceDescriptor().substring(
				handshake.getResourceDescriptor().lastIndexOf("/") + 1, handshake.getResourceDescriptor().length());// 例如页面请求：'ws://127.0.0.1:4444/'+topSid;
		// desc获取到的就是topSid。此处以desc区分不同分类的连接，用于后续通讯  userid
		
		if (socketMap.get(desc) == null || socketMap.get(desc).size() == 0) {
			List<WebSocket> list = Lists.newArrayList();
			list.add(conn);// connect成功时候执行
			socketMap.put(desc, list);
		} else {
			socketMap.get(desc).add(conn);
		}
	}

	@Override
	public void onClose(WebSocket conn, int code, String reason, boolean remote) {
		System.out.println("onClose");
		Set<String> setList = socketMap.keySet();
		for (String string : setList) {
			if (socketMap.get(string).contains(conn)) {
				socketMap.get(string).remove(conn);// 删除连接
				if (socketMap.get(string).isEmpty()) {// 连接为空时清除
					socketMap.remove(string);
				}
				break;
			}
		}
	}

	@Override
	public void onMessage(WebSocket conn, String message) {
		System.out.println("onMessage");
		System.out.println(message);
		/*
		 * List<WebSocket> socketList = socketMap.get(message); for (WebSocket
		 * webSocket : socketList) { webSocket.send("已收到~"); }
		 */
		sendInAllChannel("~已收到~");// 测试收到信息时全局发送消息
	}

	@Override
	public void onError(WebSocket conn, Exception ex) {
		System.out.println("onError");
		Set<String> setList = socketMap.keySet();
		for (String string : setList) {
			if (socketMap.get(string).contains(conn)) {
				socketMap.get(string).remove(conn);// 删除连接
				if (socketMap.get(string).isEmpty()) {// 连接为空时清除
					socketMap.remove(string);
				}
				break;
			}
		}
	}

	/**
	 * 服务端启动
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		InetSocketAddress address = new InetSocketAddress(4444);
		WebSocketUtil socket = new WebSocketUtil(address);
		socket.start();// 启动websocket服务器进程
	}

	/**
	 * 发送消息到某一个连接
	 * 
	 * @param channel
	 * @param message
	 */
	public void sendInOneChannel(String channel, String message) {
		List<WebSocket> list = socketMap.get(channel);
		if (list != null) {
			for (WebSocket webSocket : list) {
				webSocket.send(message);
			}
		}

	}

	/**
	 * 发送消息到所有连接
	 * 
	 * @param message
	 */
	public static void sendInAllChannel(String message) {
		Set<String> setList = socketMap.keySet();
		for (String string : setList) {
			List<WebSocket> list = socketMap.get(string);
			for (WebSocket webSocket : list) {
				webSocket.send(message);
			}
		}
	}

//	int i = 0;
//	Timer timer = new Timer(true);
//
//	private void test(final String desc) {
//		timer.schedule(new TimerTask() {
//
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				i++;
//				sendInOneChannel(desc, "1111111111111--" + i);
//			}
//		}, 1000, 1000);
//	}
}